(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:PreorderCtrl
     * @description
     * # PreorderCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('PreorderCtrl', PreorderCtrl);

    PreorderCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function PreorderCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);

        }

        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize,
                query: { store_id: $rootScope.store_id },
            };

            if ($scope.pre_order_id) {
                query.query.pre_order_id = $scope.pre_order_id;
            }

            dataService.preorderGet(query).then(function(re1) {
                if (re1.preorders) {
                    if ($scope.pre_order_id) {
                        $scope.preorder = re1.preorders[0];
                    } else {
                        $scope.preorders = re1.preorders;
                        $scope.total_count = re1.total_count;
                    }
                }
            });
        }


        //添加数据 初始化
        $scope.infoAction = function() {
            $scope.pre_order_id = $location.search().pre_order_id;
            if ($scope.pre_order_id) {
                $scope.getlist();
            }
            else{
                $scope.reservation ={
                    store_id:$rootScope.scope_id,
                    res_item_id:1,
                    period_id:3,
                    timeslot_id:3,
                    is_pay_deposit:1,
                }
            }
        }

        // schema_type_id
        $scope.saveData = function() {

            var zhuoziApiAry = {
                "add": "preorderAdd",
                "up": "schemaUpdate",
            }
            var api = "add";
            if ($scope.schema_id) {
                api = "up";
            }
            
            dataService[zhuoziApiAry[api]]($scope.schema).then(function(r) {
                helper.myAlert("success", null, null, function() {
                    $state.go("schema.list");
                });
            })
        }
    }
})();